package cn.tansanqinger.service.account.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.account.AccountSqlDaoImpl;
import cn.tansanqinger.dao.sql.account.UserAccountSqlDaoImpl;
import cn.tansanqinger.dao.sql.group.ReportUserAndGroupSqlDaoImpl;
import cn.tansanqinger.dao.sql.usernumber.UserNumberSqlDaoImpl;
import cn.tansanqinger.pojo.Reportuser;
import cn.tansanqinger.pojo.User;
import cn.tansanqinger.service.account.IAccountDataService;
import cn.tansanqinger.util.DateUtil;
import cn.tansanqinger.util.PageData;
import cn.tansanqinger.util.Tools;
/**
 * 该类用于对账号信息进行处理
 * @author C3H2
 * @time 2021.01
 */
@Service
public class AccountDataServiceImpl implements IAccountDataService{

	@Autowired
	private AccountSqlDaoImpl accountSql;
	@Autowired
	private UserNumberSqlDaoImpl uimpl;
	@Autowired
	private ReportUserAndGroupSqlDaoImpl rimpl;
	@Autowired
	private UserAccountSqlDaoImpl aimpl;
	
	@Override
	public Map<String, Object> selectDefaultAmount() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("teacherCheckAmount", accountSql.selectTeacherCheckAmount());
		map.put("accountReportAmount", accountSql.selectAccountReportAmount());
		map.put("accountSataeAmount", accountSql.selectAccountSataeAmount());
		map.put("pendingKnowledgeAmount", accountSql.selectPendingKnowledgeAmount());
		map.put("objectionableKnowledgeAmount", accountSql.selectObjectionableKnowledgeAmount());
		map.put("pendingTopicAmount", accountSql.selectPendingTopicAmount());
		map.put("objectionableTopicAmount", accountSql.selectObjectionableTopicAmount());
		map.put("teacherAllAmount", accountSql.selectTeacherAll());
		map.put("studentAllAmount", accountSql.selectStudentAll());
		map.put("monthBrowseAmount", accountSql.selectMonthBrowseAmount());
		map.put("yearBrowseAmount", accountSql.selectYearBrowseAmount());
		return map;
	}

	@Override
	public PageData selectDefaultNumber() {
		PageData pd = new PageData();
		String date = DateUtil.getDay();
		String year = date.substring(0, 4);
		String month = date.substring(0, 7);
		int thisDayTeacher = uimpl.thisDayTeacher(date);//当日教师新增数量
		int thisDayStudent = uimpl.thisDayStudent(date);//当日学生新增数量
		int thisMonthTeacher = uimpl.thisMonthTeacher(month);//当月教师新增数量
		int thisMonthStudent = uimpl.thisMonthStudent(month);//当月学生新增数量
		int thisYearTeacher = uimpl.thisYearTeacher(year);//当年教师新增数量
		int thisYearStudent = uimpl.thisYearStudent(year);;//当年学生新增数量
		pd.put("thisDayTeacher", thisDayTeacher);
		pd.put("thisDayStudent", thisDayStudent);
		pd.put("thisDayAll", thisDayStudent+thisDayTeacher);
		pd.put("thisMonthTeacher", thisMonthTeacher);
		pd.put("thisMonthStudent", thisMonthStudent);
		pd.put("thisMonthAll", thisMonthTeacher+thisMonthStudent);
		pd.put("thisYearTeacher", thisYearTeacher);
		pd.put("thisYearStudent", thisYearStudent);
		pd.put("thisYearAll", thisYearTeacher+thisYearStudent);
		ArrayList<String> dateMessage = new ArrayList<String>(12);
		ArrayList<Integer> dateTeacherNumber = new ArrayList<Integer>(12);
		ArrayList<Integer> dateStudentNumber = new ArrayList<Integer>(12);
		Calendar c = Calendar.getInstance();
        for(int i = 11 ; i > 0 ; i --) {
	        c.setTime(new Date());
	        c.add(Calendar.MONTH, -i);
	        String mon = DateUtil.getMonth(c.getTime());
	        dateMessage.add(mon);
	        dateStudentNumber.add(uimpl.thisMonthStudent(mon));
	        dateTeacherNumber.add(uimpl.thisMonthTeacher(mon));
        }
        dateMessage.add(DateUtil.getMonth());
        dateStudentNumber.add(thisMonthStudent);
        dateTeacherNumber.add(thisMonthTeacher);
        pd.put("dateMessage", dateMessage.toArray());
        pd.put("dateTeacherNumber", dateTeacherNumber.toArray());
        pd.put("dateStudentNumber", dateStudentNumber.toArray());
		return pd;
	}

	@Override
	public List<Map<String, Object>> reportUserMessageAll(HttpServletRequest request) {
		String id = Tools.isEmpty(request.getParameter("id"));
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
		if(id.matches("\\d+")) {
			List<Reportuser> list = rimpl.selectReport(Integer.parseInt(id), 0);
			List<Reportuser> reportusers = new ArrayList<Reportuser>();
			Map<String, Object> map = null;
			String date = "";
			for (int i = 0; i < list.size(); i++) {
				User user = aimpl.selectId(list.get(i).getRid());
				if(user!=null) {
					list.get(i).setUname("".equals(user.getName())?user.getPhone():user.getName()+"【"+user.getEmail()+"】");
				}
				if("".equals(date)) {
					date = list.get(i).getRdate().substring(0,10);
					list.get(i).setRdate(list.get(i).getRdate().substring(11).trim());
					reportusers.add(list.get(i));
				} else {
					if(date.equals(list.get(i).getRdate().subSequence(0, 10))) {
						reportusers.add(list.get(i));
					} else {
						map = new HashMap<String, Object>();
						map.put("date", date);
						map.put("message", reportusers);
						resultList.add(map);
						reportusers = new ArrayList<Reportuser>();
						date = list.get(i).getRdate();
						list.get(i).setRdate(list.get(i).getRdate().substring(11).trim());
						reportusers.add(list.get(i));
					}
				}
			}
		}
		return resultList;
	}

	@Override
	public Map<String, Object> selectUserDisposeMessageAll(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
